#include "kernel_operator.h"


constexpr int32_t TOTAL_LENGTH  = 8 * 2048;   // 复数个数
constexpr int32_t USE_CORE_NUM  = 8;
constexpr int32_t BLOCK_LENGTH  = TOTAL_LENGTH / USE_CORE_NUM; // 复数个数
constexpr int32_t TILE_NUM      = 8;
constexpr int32_t BUFFER_NUM    = 2;
constexpr int32_t TILE_LENGTH   = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; // 复数个数

class KernelComplex {
public:
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z)
    {
        // 每个核负责一段连续复数
        // x 实部：float，长度 BLOCK_LENGTH
        // y 虚部：float，长度 BLOCK_LENGTH
        // z 输出：complex64，长度 BLOCK_LENGTH
        xGm.SetGlobalBuffer((__gm__ float *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(),
                            BLOCK_LENGTH);
        yGm.SetGlobalBuffer((__gm__ float *)y + BLOCK_LENGTH * AscendC::GetBlockIdx(),
                            BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ float *)z + BLOCK_LENGTH * AscendC::GetBlockIdx()*2,
                            BLOCK_LENGTH*2);

        pipe.InitBuffer(inQueueX,  BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(inQueueY,  BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, 2*TILE_LENGTH * sizeof(float));
    }

    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; ++i) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.AllocTensor<float>();

        int32_t offset = progress * TILE_LENGTH;
        AscendC::DataCopy(xLocal, xGm[offset], TILE_LENGTH);
        AscendC::DataCopy(yLocal, yGm[offset], TILE_LENGTH);

        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }

    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float>   xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float>   yLocal = inQueueY.DeQue<float>();
        AscendC::LocalTensor<float > zLocal = outQueueZ.AllocTensor<float >();

         for (int i = 0; i < TILE_LENGTH; i++) {
            float real = xLocal.GetValue(i);
            float imag = yLocal.GetValue(i);
            zLocal.SetValue(2 * i, real);     // 存实部
            zLocal.SetValue(2 * i + 1, imag); // 存虚部
        }
        
        outQueueZ.EnQue<float>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);
    }

    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::DataCopy(zGm[2*progress * TILE_LENGTH], zLocal, 2*TILE_LENGTH);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN,  BUFFER_NUM> inQueueX, inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<float>       xGm, yGm;  
    AscendC::GlobalTensor<float> zGm;     
};

extern "C" __global__ __aicore__ void complex_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z)
{
    KernelComplex op;
    op.Init(x, y, z);
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void complex_custom_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *y, uint8_t *z)
{
    complex_custom<<<blockDim, nullptr, stream>>>(x, y, z);
}
#endif